package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.platform.entity.DeviceEventReportEntity;
import com.iwomy.secureplat.platform.service.DeviceEventReportService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 设备事件报表
 *
 * @author pig
 * @date 2023-12-29 15:16:04
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/deviceEventReport" )
@Tag(description = "deviceEventReport" , name = "设备事件报表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DeviceEventReportController {

    private final  DeviceEventReportService deviceEventReportService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param deviceEventReport 设备事件报表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_deviceEventReport_view')" )
    public R getDeviceEventReportPage(@ParameterObject Page page, @ParameterObject DeviceEventReportEntity deviceEventReport) {
        LambdaQueryWrapper<DeviceEventReportEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(deviceEventReportService.page(page, wrapper));
    }


    /**
     * 通过id查询设备事件报表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platform_deviceEventReport_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(deviceEventReportService.getById(id));
    }

    /**
     * 新增设备事件报表
     * @param deviceEventReport 设备事件报表
     * @return R
     */
    @Operation(summary = "新增设备事件报表" , description = "新增设备事件报表" )
    @SysLog("新增设备事件报表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_deviceEventReport_add')" )
    public R save(@RequestBody DeviceEventReportEntity deviceEventReport) {
        return R.ok(deviceEventReportService.save(deviceEventReport));
    }

    /**
     * 修改设备事件报表
     * @param deviceEventReport 设备事件报表
     * @return R
     */
    @Operation(summary = "修改设备事件报表" , description = "修改设备事件报表" )
    @SysLog("修改设备事件报表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_deviceEventReport_edit')" )
    public R updateById(@RequestBody DeviceEventReportEntity deviceEventReport) {
        return R.ok(deviceEventReportService.updateById(deviceEventReport));
    }

    /**
     * 通过id删除设备事件报表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除设备事件报表" , description = "通过id删除设备事件报表" )
    @SysLog("通过id删除设备事件报表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_deviceEventReport_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(deviceEventReportService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param deviceEventReport 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_deviceEventReport_export')" )
    public List<DeviceEventReportEntity> export(DeviceEventReportEntity deviceEventReport,Long[] ids) {
        return deviceEventReportService.list(Wrappers.lambdaQuery(deviceEventReport).in(ArrayUtil.isNotEmpty(ids), DeviceEventReportEntity::getId, ids));
    }
}